#include<windows.h>
#include<memory>
#include<dx11demo.h>

//系统消息处理函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT paintStruct;
	HDC hDC;

	switch (message)
	{
	case WM_PAINT:
		hDC = BeginPaint(hwnd, &paintStruct);
		EndPaint(hwnd, &paintStruct);
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	default:
		return DefWindowProc(hwnd, message, wParam, lParam);
	}

	return 0;

}




int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
	LPWSTR cmdLine, int cmdShow)
{
	UNREFERENCED_PARAMETER(prevInstance);       //避免对于未使用变量的系统警告
	UNREFERENCED_PARAMETER(cmdLine);

	WNDCLASSEX wndClass = { 0 };               //窗口的类WNDCLASSEX，并初始化
	wndClass.cbSize = sizeof(WNDCLASSEX);      //窗口大小
	wndClass.style = CS_HREDRAW | CS_VREDRAW;   //窗口风格
	wndClass.lpfnWndProc = WndProc;           //回调函数，处理系统发来的时间消息
	wndClass.hInstance = hInstance;             
	wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wndClass.lpszMenuName = NULL;
	wndClass.lpszClassName = "DX11BookWindowClass";      //窗口的名字

	if (!RegisterClassEx(&wndClass))				//注册窗口，创造窗口前必须注册
		return -1;

	RECT rc = { 0,0,640,480 };
	AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);

	HWND hwnd = CreateWindowA("DX11BookWindowClass", "Blank Win32 Window",
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left,
		rc.bottom - rc.top, NULL, NULL, hInstance, NULL);       //创造窗口

	if (!hwnd)
		return -1;

	ShowWindow(hwnd, cmdShow);        //显示窗口

	std::auto_ptr<Dx11DemoBase> demo(new Dx11DemoBase());
	// Demo Initialize
	bool result = demo->Initialize(hInstance, hwnd);
	
	if (!result)
		return -1;

	//Demo 初始化
	MSG msg = { 0 };
	while (msg.message != WM_QUIT)
	{
		if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			//更新
			demo->Update(0.0f);
			demo->Render();
			//画图
		}
	}

	//Demo 关闭
	demo->Shutdown();


	 return static_cast<int>(msg.wParam);
}